﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SharpDiary.Core.Controls
{
    public partial class DateTimeBrowser : UserControl
    {
        private DateTime _value = DateTime.Now;

        public DateTime Value
        {
            get { return _value; }
            set
            {
                if (value.Date != _value.Date)
                {
                    _value = value;

                    OnValueChanged(this, new DateTimeValueChangedEventArgs(value));
                }
            }
        }

        private ListBoxForDTB _yearControl = new ListBoxForDTB();
        private ListBoxForDTB _monthControl = new ListBoxForDTB();
        private ListBoxForDTB _dayControl = new ListBoxForDTB();

        private List<int> _years = new List<int>();
        private List<int> _months = new List<int>();
        private List<int> _days = new List<int>();

        public DateTimeBrowser()
        {
            InitializeComponent();

            _years.AddRange(new int[] { DateTime.Now.Year });

            _yearControl.Pressed += new EventHandler<PressedEventArgs>(_yearControl_Pressed);
            _monthControl.Pressed += new EventHandler<PressedEventArgs>(_monthControl_Pressed);
            _dayControl.Pressed += new EventHandler<PressedEventArgs>(_dayControl_Pressed);

            _yearControl.DisplayYears(_years);
            _monthControl.DisplayMonths();
            _dayControl.DisplayDays(DateTime.Now.Year, DateTime.Now.Month);

            FlowContainerMain.ControlCurrent = _yearControl;
        }

        /// <summary>
        /// 当前选择的日期改变时触发此事件。
        /// </summary>
        public event EventHandler<DateTimeValueChangedEventArgs> ValueChanged;

        protected virtual void OnValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            if (ValueChanged != null)
            {
                ValueChanged(sender, e);
            }
        }

        void _dayControl_Pressed(object sender, EventArgs e)
        {
            if (_dayControl.listBoxMain.SelectedItem != null)
            {
                btnDay.Text = _dayControl.listBoxMain.SelectedItem.ToString();
                UpdateValue();
            }

            //切换到年视图
            //FlowContainerMain.FlowForward(_yearControl);
        }

        void _monthControl_Pressed(object sender, PressedEventArgs e)
        {
            if (_monthControl.listBoxMain.SelectedItem != null)
            {
                btnMonth.Text = _monthControl.listBoxMain.SelectedItem.ToString();
                UpdateValue();
            }

            if (_yearControl.listBoxMain.SelectedItem != null && _monthControl.listBoxMain.SelectedItem != null)
            {
                _dayControl.DisplayDays((_yearControl.listBoxMain.SelectedItem as DateValue).Value,
                    (_monthControl.listBoxMain.SelectedItem as DateValue).Value);
                FlowContainerMain.FlowForward(_dayControl);
            }
        }

        private void _yearControl_Pressed(object sender, PressedEventArgs e)
        {
            if (_yearControl.listBoxMain.SelectedItem != null)
            {
                btnYear.Text = _yearControl.listBoxMain.SelectedItem.ToString();
                UpdateValue();
            }


            FlowContainerMain.FlowForward(_monthControl);
        }

        /// <summary>
        /// 更新选择的日期。
        /// </summary>
        private void UpdateValue()
        {
            int year = DateTime.Now.Year;
            int month = 1;
            int day = 1;

            if (_yearControl.listBoxMain.SelectedItem != null)
            {
                year = (_yearControl.listBoxMain.SelectedItem as DateValue).Value;
            }

            if (_monthControl.listBoxMain.SelectedItem != null)
            {
                month = (_monthControl.listBoxMain.SelectedItem as DateValue).Value;
            }

            if (_dayControl.listBoxMain.SelectedItem != null)
            {
                day = (_dayControl.listBoxMain.SelectedItem as DateValue).Value;
            }

            Value = new DateTime(year, month, day);
        }

        private void btnYear_Click(object sender, EventArgs e)
        {
            FlowContainerMain.FlowBackword(_yearControl);
        }

        private void btnMonth_Click(object sender, EventArgs e)
        {
            if (FlowContainerMain.DisplayedControl == _dayControl)
            {

                FlowContainerMain.FlowBackword(_monthControl);
            }
            else
            {

                FlowContainerMain.FlowForward(_monthControl);
            }
        }

        private void btnDay_Click(object sender, EventArgs e)
        {
            FlowContainerMain.FlowForward(_dayControl);
        }
    }

    public class DateTimeValueChangedEventArgs : EventArgs
    {
        private DateTime _value = DateTime.Now;

        public DateTime Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public DateTimeValueChangedEventArgs() { }

        public DateTimeValueChangedEventArgs(DateTime value) { _value = value; }
    }
}
